package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.queryObject.StrategyQueryObject;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.vo.StrategyCatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IDestinationService destinationService;

    /**
     * 分页映射方法
     * @param model
     * @param qo    分页条件
     * @return
     */
    @RequestMapping("/list")
    public Object list(Model model, @ModelAttribute("qo")StrategyQueryObject qo){
        // Page : spring-data 提供用于 mongodb 分页的类
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page",page);
        return "strategy/list";
    }

    /**
     * 跳板,到 input 页面
     * @param id 要编辑的用户 id
     * @param model model模型
     * @return
     */
    @RequestMapping("/input")
    public String input(String id,Model model){

        List<StrategyCatalogVO> catalogs = strategyCatalogService.queryCatalogs();

        model.addAttribute("catalogs",catalogs);
        model.addAttribute("themes", strategyThemeService.list());

        // 如果 id 不为空的话 , 在编辑页面回显数据
        if(StringUtils.hasLength(id)){
            model.addAttribute("strategy",strategyService.get(id));
        }

        return "strategy/input"; //找视图

    }


    /**
     * 保存或更新
     * @param strategy    要保存的数据
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy){
        // 添加和保存的时候需要维护冗余字段

        // destId 设置关联目的地的id
        StrategyCatalog strategyCatalog = strategyCatalogService.get(strategy.getCatalogId());
        strategy.setDestId(strategyCatalog.getDestId());
        strategy.setDestName(strategyCatalog.getDestName());

        // 关联主题的名字
        strategy.setThemeName(strategyThemeService.get(strategy.getThemeId()).getName());
        // 关联分类的名字
        strategy.setCatalogName(strategyCatalogService.get(strategy.getCatalogId()).getName());

        // 判断是国内还是国外
        String destId = strategyCatalogService.get(strategy.getCatalogId()).getDestId();
        List<Destination> toasts = destinationService.getToasts(destId);
        if (!"中国".equals(toasts.get(0).getName())){
            strategy.setIsabroad(Strategy.ABROAD_YES);
        }else {
            strategy.setIsabroad(Strategy.ABROAD_NO);
        }

        if (StringUtils.hasLength(strategy.getId())){

            Strategy temp = strategyService.get(strategy.getId());

            // 更新的时候要注意参数丢失
            strategy.setViewnum(temp.getViewnum());
            strategy.setReplynum(temp.getReplynum());
            strategy.setFavornum(temp.getFavornum());
            strategy.setSharenum(temp.getSharenum());
            strategy.setThumbsupnum(temp.getThumbsupnum());

            strategy.setCreateTime(temp.getCreateTime());
            strategyService.update(strategy);
        }else {
            // 保存
            // 保存的时候需要设置保存的时间
            strategy.setCreateTime(new Date());
            strategyService.save(strategy);
        }
        return JsonResult.success();
    }


    /**
     * 删除方法
     * @param id    要删除区域的id
     * @return  JsonResult
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        strategyService.delete(id);
        return JsonResult.success();
    }




}
